import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/constant/constant.dart';

import 'package:flutter_ncsjqz_getx/http/dio_service.dart';
import 'package:flutter_ncsjqz_getx/model/base_entity.dart';
import 'package:flutter_ncsjqz_getx/model/circle_list_entity.dart';
import 'package:flutter_ncsjqz_getx/model/circle_type_entity.dart';
import 'package:flutter_ncsjqz_getx/model/square_a_d_entity.dart';
import 'package:flutter_ncsjqz_getx/model/sys_notice_entity.dart';
import 'package:package_get/base_logic.dart';

class SquareLogic extends BaseLogic {
  int currentIndex = 0;
  int pageIndex = 0;
  int pageSize = 20;
  List<String> type = [];
  List<CircleListDataList> circleListDataList = [];
  TextEditingController textEditingControllerSearch = TextEditingController();
  List<SysNoticeData>? sysNoticeData = [SysNoticeData()..msgTitle=''];
  List<SquareADData>? squareADData = [];

  /// 分类切换
  setCurrentIndex(value){
    currentIndex = value;
    update();
    getBodyList();
  }

  getData() async{
    getImages();
    getSysNotice();
    // 先获取分类才能获取分类下的数据
    await getPostType();
    getBodyList();
  }

  /// 计算每个帖子的评论数
  int getCount(index){
    int count = 0;
    // 评论数
    count = circleListDataList[index].comments!.length;
    // 累计回复数
    circleListDataList[index].comments!.forEach((element) => count += element.replys!.length);
    return count;
  }

  /// 获取帖子分类
  getPostType() async{
    CircleTypeEntity circleTypeEntity = await DioService<CircleTypeEntity>().postCategory();
    if(circleTypeEntity.code == 0) circleTypeEntity.data!.forEach((element) => type.add(element.categoryName ?? ''));
    update();
  }

  /// 获取系统公告
  getSysNotice() async{
    SysNoticeEntity sysNoticeEntity = await DioService<SysNoticeEntity>().v2_listSysNotice();
    if(sysNoticeEntity.code == 0) sysNoticeData = sysNoticeEntity.data;
    update();
  }

  /// 获取内容
  getBodyList({bool isLoad:false}) async{
    Map<String,dynamic> circleSearch = {
      "pageSize": pageSize,
      "currPage": isLoad ? pageIndex++ : 1,
      "postTypeName": type[currentIndex],
    };
    if(textEditingControllerSearch.value.text != "") {
      circleSearch["postContent"] = textEditingControllerSearch.value.text;
    }
    CircleListEntity circleListEntity = await DioService<CircleListEntity>().indexPage(circleSearch);
    if(circleListEntity.code == 0) circleListDataList = circleListEntity.data!.xList ?? [];
    update();
  }

  /// 获取轮播图
  getImages() async{
    SquareADEntity squareADEntity = await DioService<SquareADEntity>().advertisingCarousel();
    if(squareADEntity.code == 0) squareADData = squareADEntity.data;
    update();
  }

  /// 点赞
  praise(postId,index) async{
    Map<String,dynamic> data = {
      "postId": postId,
      "userId":  Constant.userDetailData!.id,
    };
    BaseEntity baseEntity = await DioService<BaseEntity>().praise(data);
    if(baseEntity.code == 0){
      if(circleListDataList[index].isPraise!){
        circleListDataList[index].isPraise = false;
        circleListDataList[index].praiseCount = circleListDataList[index].praiseCount!-1;
      }else{
        circleListDataList[index].isPraise = true;
        circleListDataList[index].praiseCount = circleListDataList[index].praiseCount!+1;
      }
      update();
    }
  }
}
